# @Time: 2021/3/3 15:10
# @Athuor: 雷磊
# @Email: 1021010830@qq.com
# @File: utils.py
import hashlib
from setting import DATA_PATH, BASE_PATH, CASE_PATH
import os


#设置转换返回报文的函数
def change_resp(resp):
    if resp:
        return resp.replace('":','=')

def set_resp_data(resp):
    if resp:
        return resp.lower().replace('":"', '=').replace('":', '=')

def set_res_data(res):
    if res:
        return res.lower().replace('":"', '=').replace('":', '=')

# def set_res_data(resp):
#     json数据方式进行处理后进行断言
#     return ['{}={}'.format(key, resp[key]) for key in resp.keys()]

def hash_code(passwd):
    md5 = hashlib.md5()
    md5.update(passwd.encode("utf-8"))
    return md5.hexdigest()


def gen_cases(template_name):
    files = os.listdir(DATA_PATH)
    files = {file for file in files if file.endswith('.yaml') or file.endswith('yml')}
    for file in files:
        file_name = file.split('.')[0]
        # 去掉下划线.单词字母大写
        class_name = "".join([c.capitalize() for c in file_name.split('_')])
        yaml_name = file
        case_name = file_name
        yaml_name = file
        url_name = file.split('.')[0].split('_')[1]
        module_name ="{}.py".format(file_name)
        template_path = os.path.join(BASE_PATH,template_name)
        with open(template_path) as f:
            data = f.read() % {
                'class_name': class_name,
                'yaml_name': yaml_name,
                'case_name': case_name
            }
        module_path = CASE_PATH + '/' + module_name
        with open(module_path, 'w', encoding='utf-8') as f:
            f.write(data)

